package com.jing.myblogsm.controller;

import com.jing.myblogsm.common.AppVariable;
import com.jing.myblogsm.common.PasswordUtils;
import com.jing.myblogsm.common.ResultAjax;
import com.jing.myblogsm.model.Userinfo;
import com.jing.myblogsm.model.vo.UserinfoVo;
import com.jing.myblogsm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.geom.GeneralPath;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param userinfo
     * @return
     */
    @RequestMapping("/reg")
    @PostMapping
    public ResultAjax reg(Userinfo userinfo) {
        // 1.校验参数
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername())
            || !StringUtils.hasLength(userinfo.getPassword())) {
            // 参数异常
            return ResultAjax.fail(-1,"非法参数");
        }
        // 密码加盐
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        // 2.调用service
        int result = userService.reg(userinfo);
        // 3.将执行结果返回给前端
        return ResultAjax.succ(result);
    }

    @RequestMapping("/login")
    @GetMapping
    public ResultAjax getUserByName(HttpServletRequest request, UserinfoVo userinfoVo) {
        // 1.非法校验
        if (userinfoVo == null || !StringUtils.hasLength(userinfoVo.getUsername()) ||
            !StringUtils.hasLength(userinfoVo.getPassword())) {
            // 非法登录
            return ResultAjax.fail(-1,"参数有误!");
        }
        // 2.根据用户名查询对象
        Userinfo userinfo = userService.getUserByName(userinfoVo.getUsername());
        if (userinfo == null || userinfo.getId() <= 0) {
             // 不存在此用户
            return ResultAjax.fail(-2,"用户名或密码错误");
        }
        // 3.使用对象中的密码和用户输入的密码进行比较
        // 加盐解密
        if (!PasswordUtils.decrypt(userinfoVo.getPassword(),userinfo.getPassword())) {
            // 密码不正确
            return ResultAjax.fail(-2,"用户名或密码错误");
        }
        // 4.比较成功后,将对象存储到session中
        HttpSession session = request.getSession();
        session.setAttribute(AppVariable.SESSION_USERINFO_KEY,userinfo);
        // 5.将结果返回给用户
        return ResultAjax.succ(1);
    }

    /**
     * 注销
     */
    @RequestMapping("/logout")
    public ResultAjax logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null || session.getAttribute(AppVariable.SESSION_USERINFO_KEY) != null) {
            session.removeAttribute(AppVariable.SESSION_USERINFO_KEY);
        }
        return ResultAjax.succ(1);
    }
}
